#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:MaLiang
# datetime:2020/8/18 17:33
# software: PyCharm
# 爬取 中国台风网数据

import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import traceback


# logging.basicConfig函数对日志的输出格式及方式做相关配置
logging.basicConfig(level=logging.DEBUG,
                   format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
## url ：获取源码的网址
def getdata(url,table_id):
    ## 浏览器驱动
    driver = webdriver.Chrome()
    ## 获取网页
    #driver.get(url)
    driver.get('D:\\taifeng.html')
    ## 获取源码的 的数据模块 也就是：div class="container"
    ##
    try:
        container = driver.find_element_by_class_name('container')
        html = container.get_attribute('innerHTML')
        #logging.info('==========台风预报HTML: %s' %html)

        # 获取台风预报  container标签模块 当前台风信息
        #
        #
        tyjsoncon = driver.find_element_by_xpath("//input[@name='tyjsoncon']").get_property('value')
        # tyjsoncon 返回值为json 数据
        #logging.info("==========当前台风信息value: %s" %tyjsoncon)

        ## 判断传入的数据表ID
        # currTypoonDetail ---台风实况路劲
        # currTypoonYB     ---预报信息

        #获取对应表单数据
        logging.info("============开始获取 %s 模块数据============")
        tr_list = driver.find_element(By.ID,table_id).find_elements(By.TAG_NAME,'tr')
        #logging.info("============台风预报返回数据: %s" %tr_list)
        # 存放结果的数据
        row_list = []
        for tr in tr_list:
            td_list = tr.find_elements(By.TAG_NAME,'td')
            logging.info("============td_list 返回数据: %s" % td_list)
            for td in td_list:
                print(td.text)
                row_list.append(td.text)
        logging.info("============获取 %s 模块数据完成============")

    except Exception as e:
        traceback.print_exc()
        logging.info(e)

if __name__ == '__main__':
    ## 中国台风网url
    url = 'http://www.readearth.com/publictyphoon/'
    # currTypoonDetail ---台风实况路劲
    # currTypoonYB     ---预报信息
    sk_table_id = 'currTypoonDetail'
    yb_table_id = 'currTypoonYB'
    getdata(url,yb_table_id)
